package CGI::FOGE2::ViewGenerator;
use strict;
use CGI;
use CGI::FOGE2::Form;
use CGI::FOGE2::Tools;

our @ISA = ();

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my %args = @_;

    my $self = {
	form => $args{form},
	submit => 'see',
    };
    
    bless $self, $class;
    return $self;
}

sub serve {
    my $self = shift;
    my $form = $self->{form};
    my $out;
    foreach my $section (@{$form->{sections}}) {
	print $self->serve_section($section);
    }
    return $out;
}

sub serve_section {
    my $self = shift;
    my $section = shift;

    my $secname = $section->get_table_name();

    my $has_prolog = defined $section->{prolog};
    my $has_epilog = defined $section->{epilog};

    my $out = '';
    my $check = CGI::checkbox ( -name => 'showsec_' . $secname,
				-checked => 1,
				-label => '');

    $out .= CGI::h2({-class => "sectitle"}, $check, $section->{title});
    $out .= CGI::span({-class => "sechead"},  $section->{prolog}) if $has_prolog;

    my $question_out = '';
    foreach my $question (@{$section->{questions}}) {
        $question_out .= $self->serve_question($question);
    }
    $out .= CGI::table($question_out);
    $out .= CGI::span({-class => "sechead"},  $section->{epilog}) if $has_epilog;

    return $out;   
}

sub serve_question {
    my $self = shift;
    my $question = shift;
    my $names = $question->generate_field_names();
    my $name = 'showq_' . $names->[0];

    return table3cols($question->{title}, $question->{ismandatory},
		      CGI::checkbox( -name => $name,
				     -checked => 1,
				     -label => ''));
}

1;

